<?php

	include_once('../config.php'); 
	session_start();
	LinkDataBase();
			
	$Array_DBTableName = DBTableNameGet($_SESSION['ID'], $_POST['year'], $_POST['season'], $_POST['tablename']);
	$filename = $Array_DBTableName[0];
	
	//Find question.
	$sql = "select * from ".$filename." LEFT JOIN sheet ON ".$filename.".table_name = binary(sheet.table_name) 
			WHERE table_number='".$_POST['tablename']."' AND 
			item NOT LIKE '%Base: All Respondents%' AND 
			item NOT LIKE '%Base: Respondents%'";
	
	$result= mysql_query($sql);
	$numResult = mysql_num_rows($result);
			
	if ($numResult > 0) {
		?>
		<script type="text/javascript">
		var data = new google.visualization.DataTable();
		var myrow = new Array('');
						
        data.addColumn('string', 'string');
		<?php
		$item = "";
		while($row = mysql_fetch_assoc($result)) {
			if ((($item!="")&&($row['item']!='Sigma')&&
				(strpos($row['total'],'%'))&&
				($_POST['keyword']==''))||
				//For keyword exists.
				(($item!="")&&($row['item']!='Sigma')&&
				(strpos($row['total'],'%'))&&
				($_POST['keyword']!='')&&
				//contain is YES.
				(((strpos($item,(string)$_POST['keyword']))&&
				($_POST['contain']=='YES'))||
				//contain is NO.
				((!strpos($item,(string)$_POST['keyword']))&&
				($_POST['contain']=='NO'))))) {
		?>
				data.addColumn('number', '<?php echo str_replace("'","\'",str_replace('(Net)','',$item)); ?>');
        		data.addColumn({type:'string', role:'tooltip'}); //Tooltip with percentages
								
				var value = <?php echo str_replace('%','',$row['total'])/100; ?>;
				myrow.push(value, Math.round(value*100)+'%');			
		<?php
			}
			else if ($row['item']=='Sigma')
				break;
							
			$item = $row['item'];
		}
		?>
						
        // A column for custom tooltip content
		data.addRows([myrow]);
        
		var options = {
			legend: {position: 'right', textStyle: {color: 'blue', fontSize: 10}},
			hAxis: {textPosition:'in', maxValue:0.1, minValue:0, format:'#%'},
			chartArea: {left:0,height:"100%"}
			};
			
		var chart = new google.visualization.BarChart(document.getElementById('<?php echo $_POST['season'].$_POST['tablename']; ?>'));
		chart.draw(data, options);
        
		function wordwrap( str, width, brk, cut ) {
		
			brk = brk || '\n';
		
			width = width || 75;
		
			cut = cut || false;
		
			if (!str) { return str; }
		
			var regex = '.{1,' +width+ '}(\\s|$)' + (cut ? '|.{' +width+ '}|.+$' : '|\\S+?(\\s|$)');
		
		
			return str.match( RegExp(regex, 'g') ).join( brk );
		
		}
		</script>
<?php
	}
?> 
<div id="<?php echo $_POST['season'].$_POST['tablename']; ?>" style="width:100%;"></div>